import util from "../../utils/util";
import {routerFilter} from '../../utils/routerFilter';
import consts from "../../js/const";

routerFilter({

    /**
     * 页面的初始数据
     */
    data: {
        completeImageUrl: consts.getImageUrl('completeOrder.png'),
        markers: []

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function (eycOrder) {
            const normalCallout = {
                id: 1,
                latitude: eycOrder.lat,
                longitude: eycOrder.lng,
                iconPath: consts.getImageUrl('location-1.png'),
                callout: {
                    content: eycOrder.company,
                    color: '#ff0000',
                    fontSize: 14,
                    borderWidth: 1,
                    borderRadius: 10,
                    borderColor: '#999999',
                    bgColor: '#fff',
                    padding: 5,
                    display: 'ALWAYS',
                    textAlign: 'center'
                }
            }
            let markers = [normalCallout];
            that.setData({eycOrder: eycOrder, markers: markers});
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.back();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    payOrder(e) {
        let eycOrder = e.currentTarget.dataset.eycOrder;
        let payParams = {
            totalFee: eycOrder.price * 100,
            openid: eycOrder.openid,
            outTradeNo: eycOrder.orderNum
        };
        util.promise('/pay/payOrder', payParams).then(res => {
            let data = res.data;
            if (data) {
                wx.requestPayment({
                    timeStamp: data.timeStamp,
                    nonceStr: data.nonceStr,
                    package: data.packageValue,
                    signType: data.signType,
                    paySign: data.paySign,
                    success(res) {
                        wx.switchTab({
                            url: '/pages/my/my'
                        })
                    },
                    fail(res) {
                        console.log(res);
                    }
                })
            }
        })
    },
    back(event) {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },
    markertap(event) {
        let that = this;
        let eycOrder = that.data.eycOrder;
        wx.openLocation({
            latitude: eycOrder.lat,
            longitude: eycOrder.lng,
            name: eycOrder.company,
            scale: 16
        })

    },
    callouttap(event) {
        this.markertap(event);
    }
});